package br.ucb.ucblive.HIB;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import br.ucb.ucblive.beans.Disciplina;
import br.ucb.ucblive.beans.Postagem;
import br.ucb.ucblive.beans.Topico;

public class TopicoHIB {
	@SuppressWarnings("unchecked")
	public List<Topico> listar() {
		Session session = HibernateUtil.getSession();
		try {
			return session.createCriteria(Topico.class).list();
		} finally {
			session.close();
		}
	}
	
	
	public void salvar(Topico topico) {
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		session.saveOrUpdate(topico);
		transaction.commit();
		session.close();
	}
	
	public void excluir(Topico obj) {
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		session.delete(obj);
		transaction.commit();
		session.close();
	}
	
	public Topico consultar(int idTopico) {
		Session session = HibernateUtil.getSession();
		try {
			Topico topico = (Topico) session.get(Topico.class,idTopico);
			return topico;
		} finally {
			session.close();
		}
	}


	@SuppressWarnings("unchecked")
	public List<Topico> listarTopicosByDisciplina(Disciplina disciplina) {
		Session session = HibernateUtil.getSession();
		try {
			return session.createCriteria(Topico.class).
					createAlias("disciplina", "disciplina").
					add(Restrictions.eq("disciplina.idDisciplina", disciplina.getIdDisciplina())).
					list();
		} finally {
			session.close();
		}
	}
}
